from urllib import request
from io import BytesIO
import gzip
from lxml import etree
from bs4 import BeautifulSoup


url = 'https://gaokao.chsi.com.cn/sch/schoolInfoMain--schId-1.dhtml'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Language': "zh-CN,zh;q=0.9",
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive',
    'Host': 'gaokao.chsi.com.cn',
}
Request = request.Request(url, headers=headers)
response = request.urlopen(Request)
html = response.read()
buff = BytesIO(html)
f = gzip.GzipFile(fileobj=buff)
html = f.read().decode('utf-8')

soup = BeautifulSoup(html, "lxml")
Map = {}
contents = soup.find_all('ul', {'class': 'clearfix'})[0]
for li in contents.find_all('li'):
    name = li.find_all('div', {'class': 'yxk-myd-name'})[0].get_text()
    echarts = li.find_all('div', {'class': 'yxk-myd-echarts'})[0]['data-id']
    Map[name] = echarts
pingjia = []
zypm = soup.find_all('div', {'class': 'yxk-col yxk-zypm clearfix'})[0]
for div in zypm.find_all('div', {'class': 'col-list'}):
    List = []
    for tr in div.find_all('tr'):
        dic = {'排名': tr.find_all('span', {'class': 'index'})[0].get_text().replace('\n', '').replace('\r', '').strip(),
               '专业': tr.find_all('div', {'class': 'overf'})[0].get_text().replace('\n', '').replace('\r', '').strip(),
               '评分': tr.find_all('span', {'class': 'avg_rank'})[0].get_text().replace('\n', '').replace('\r', '').strip(),
               '评分人数': tr.find_all('span', {'class': 'vote_num_detail'})[0].get_text().replace('\n', '').replace('\r', '').strip()}
        List.append(dic)
    pingjia.append(List)
for div in zypm.find_all('div', {'class': 'col-list last-list'}):
    List = []
    for tr in div.find_all('tr'):
        dic = {'排名': tr.find_all('span', {'class': 'index'})[0].get_text(),
               '专业': tr.find_all('div', {'class': 'overf'})[0].get_text().replace('\n', '').replace('\r', '').strip(),
               '评分': tr.find_all('span', {'class': 'avg_rank'})[0].get_text().replace('\n', '').replace('\r', '').strip(),
               '评分人数': tr.find_all('span', {'class': 'vote_num_detail'})[0].get_text().replace('\n', '').replace('\r', '').strip()}
        List.append(dic)
    pingjia.append(List)
Map['专业满意度'] = pingjia[0]
Map['专业推荐指数'] = pingjia[1]
Map['专业推荐人数'] = pingjia[2]
print(Map)



